home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume91 / utilitys / makemenu / part01 / testmenu.c < prev    next >
C/C++ Source or Header  |  1991-03-04  |  6KB  |  192 lines

  1. /* TestMenu.c V1.0 (14-oct-90) - by Hans Jansen
  2.  *
  3.  * Program to exercise the output from MakeMenu V1.0, enabling you to
  4.  * quickly prototype a menu for your own application program.
  5.  *
  6.  * This program was adapted from the RKM example Menus.c (from chapter
  7.  * Intuition:Menus, page 125-133; also published on Fish Disk #344, the
  8.  * "Libs&DevsCompanion"). The original program carried the following
  9.  * copyright statement:
  10.  */
  11.  
  12. /* Copyright (c) 1990 Commodore-Amiga, Inc.
  13.  *
  14.  * This example is provided in electronic form by Commodore-Amiga, Inc. for
  15.  * use with the 1.3 revisions of the Addison-Wesley Amiga reference manuals. 
  16.  * The 1.3 Addison-Wesley Amiga Reference Manual series contains additional
  17.  * information on the correct usage of the techniques and operating system
  18.  * functions presented in this example.  The source and executable code of
  19.  * this example may only be distributed in free electronic form, via bulletin
  20.  * board or as part of a fully non-commercial and freely redistributable
  21.  * diskette.  Both the source and executable code (including comments) must
  22.  * be included, without modification, in any copy.  This example may not be
  23.  * published in printed form or distributed with any commercial product.
  24.  * However, the programming techniques and support routines set forth in
  25.  * this example may be used in the development of original executable
  26.  * software products for Commodore Amiga computers.
  27.  * All other rights reserved.
  28.  * This example is provided "as-is" and is subject to change; no warranties
  29.  * are made.  All use is at your own risk.  No liability or responsibility
  30.  * is assumed.
  31.  */
  32.  
  33. #include <exec/types.h>
  34. #include <intuition/intuition.h>
  35. #include <intuition/intuitionbase.h>
  36. #include <libraries/dos.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40.  
  41. /* Use lowest non-obsolete version that supplies the functions you need. */
  42. #define LIB_REV 33
  43.  
  44. /* prototypes */
  45. UBYTE    handleIDCMP(struct Window *);
  46. VOID     OpenAll(VOID);
  47. VOID     cleanExit(int);
  48.  
  49. /* Globals */
  50. struct   IntuitionBase *IntuitionBase = NULL;
  51. struct   GfxBase       *GfxBase = NULL; 
  52. struct   Window *window = NULL;
  53.  
  54. struct IntuiText WinText =
  55.        {3, 0, JAM2, 20, 20, NULL, "Activate MenuItem or CloseGadget",NULL};
  56.  
  57. struct NewWindow NewWindow = {
  58.     0, 10, 640, 100, 2, 1, MENUPICK|CLOSEWINDOW,
  59.     WINDOWDRAG|WINDOWDEPTH|WINDOWCLOSE|ACTIVATE|NOCAREREFRESH,
  60.     NULL, NULL, "TestMenu V1.0 (12-oct-90) - by Hans Jansen",
  61.     NULL, NULL, 0, 0, -1, -1, WBENCHSCREEN
  62.     };
  63.  
  64. extern struct Menu *MyMenu;
  65.  
  66. VOID main (int argc, char *argv[])
  67. {
  68. /* Declare variables here */
  69. ULONG  signalmask, signals;
  70. UBYTE  done = 0;
  71.  
  72. OpenAll();
  73.  
  74. /* Set up the signals that you want to hear about ... */
  75. signalmask = 1L << window->UserPort->mp_SigBit;
  76.  
  77. /* And wait to hear from your signals */      
  78. while( !done )
  79.    {
  80.    signals = Wait(signalmask);    
  81.    if(signals & signalmask)
  82.       done = handleIDCMP(window);
  83.    };
  84.  
  85. /* Exit the program */
  86. cleanExit(RETURN_OK);
  87. }
  88.  
  89.  
  90. /* Handle the IDCMP messages */
  91. UBYTE handleIDCMP( struct Window *win )
  92. {
  93. UBYTE  flag = 0;
  94. USHORT code, selection, flags;
  95. struct IntuiMessage *message = NULL;
  96. ULONG  class, menuNum, itemNum, subNum;
  97.  
  98. /* Examine pending messages */
  99. while(message = (struct IntuiMessage *)GetMsg(win->UserPort))
  100.    {
  101.    class = message->Class;
  102.    code = message->Code;
  103.  
  104. /* When we're through with a message, reply */
  105.    ReplyMsg((struct Message *)message);
  106.    
  107. /* See what events occurred */
  108.    switch( class )
  109.       {
  110.       case CLOSEWINDOW:
  111.          flag = 1;
  112.          break;
  113.       case MENUPICK:
  114.          selection = code;
  115.          while(selection != -1) /* MENUNULL does not work for PDC V3.33... */
  116.             {
  117. /*    (uncomment this to see why...)
  118.         printf ("selection = %x, MENUNULL = %x\n", selection, MENUNULL);
  119. */
  120.             menuNum = MENUNUM(selection);
  121.             itemNum = ITEMNUM(selection);
  122.             subNum  = SUBNUM(selection);
  123.             flags = ((struct MenuItem *)
  124.                     ItemAddress(MyMenu,(LONG)selection))->Flags;
  125.         /* Show the selection (in the invoking CLI window!) */
  126.             printf("Selected menu %d, item %d", menuNum, itemNum);
  127.         if (subNum != NOSUB) printf (", subitem %d", subNum);
  128.             if(flags&CHECKED) printf(" (Checked) ");
  129.         printf ("\n");
  130.         /* See if there are more selections */
  131.             selection = ((struct MenuItem *)ItemAddress
  132.               (MyMenu,(LONG)selection))->NextSelect;
  133.          } /* end while */
  134.          break; /* case of MENUPICK */
  135.       default:
  136.          break;
  137.       } /* end switch */
  138.    } /* end while */
  139. return(flag);
  140. }
  141.  
  142.  
  143. /* Open the needed libraries, windows, etc. */
  144. VOID OpenAll (VOID)
  145. {
  146. /* Open the Intuition Library */
  147. IntuitionBase = (struct IntuitionBase *)
  148.     OpenLibrary( "intuition.library",LIB_REV);
  149. if(IntuitionBase == NULL)
  150.     cleanExit(RETURN_WARN);
  151.  
  152. /* Open the Graphics Library */
  153. GfxBase = (struct GfxBase *)
  154.     OpenLibrary("graphics.library", LIB_REV);
  155. if(GfxBase == NULL)
  156.     cleanExit(RETURN_WARN);
  157.  
  158. /* Open the window */
  159. window = OpenWindow(&NewWindow);
  160. if(window == NULL)
  161.     cleanExit(RETURN_WARN);
  162.  
  163. /* Give a brief explanation of the program */
  164. PrintIText(window->RPort,&WinText,0,0);
  165.  
  166. /* attach the menu to the window */
  167. SetMenuStrip(window, MyMenu);
  168. }
  169.  
  170.  
  171. /* Free up all the resources that we where using */
  172. VOID cleanExit (int returnValue)
  173. {
  174. if(window)
  175.    {
  176. /* If there is a menu strip, then remove it */
  177.    if(window->MenuStrip)
  178.       ClearMenuStrip(window);
  179.  
  180. /* Close the window */
  181.    CloseWindow(window);
  182.    }
  183.  
  184. /* Close the library, and then exit */
  185. if(GfxBase)
  186.    CloseLibrary((struct Library *)GfxBase);
  187.  
  188. if(IntuitionBase)
  189.    CloseLibrary((struct Library *)IntuitionBase);
  190. exit(returnValue);
  191. }
  192.